<template>

  <!-- 表格名称 -->
  <div class="title-box">
    <div class="title-name">{{ tableTitle }}</div>
    <div class="title-btn">
      <slot name="title-btn"></slot>
    </div>
  </div>

  <!-- 表格 -->
  <el-table v-loading="loading" :data="tableData" @selection-change="handleSelectionChange">
    <!-- 动态复选框列 -->
    <el-table-column
        type="selection"
        width="55"
        v-if="batchMode"
    ></el-table-column>

    <el-table-column
        :label="title_name"
        :prop="index"
        v-for="(title_name, index) in tableTitleList"
        :key="index"
        width="auto"
    ></el-table-column>
    <slot name="table-btn"></slot>

    <template #empty>
      <el-empty description="没有数据"/>
    </template>
  </el-table>

  <!-- 分页 -->
  <div class="demo-pagination-block">
    <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[5, 10, 15, 20]"
        :size="size"
        :disabled="disabled"
        :background="background"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="sizeChange"
        @current-change="currentChange"
    />
  </div>
</template>

<script setup>
import {ref} from 'vue'

// 以下定义是组件之间传参的参数
const Props = defineProps({
  tableTitle: {
    required: true,
    type: String
  },

  loading: {
    required: true,
    type: Boolean
  },

  tableTitleList: {
    required: true
  },

  tableData: {
    required: true
  },

  total: {
    required: true
  },

  currentPage: {
    required: true
  },
  pageSize: {
    required: true
  },

  batchMode: {required: false, type: Boolean, default: false}, // 是否启用批量操作模式
})
const emit = defineEmits(['sizeChange', 'currentChange'])

// 以下是分页参数
// const currentPage = ref(1)//当前页码
// const pageSize = ref(5)//每页显示的条目数
const size = ref()//分页组件的大小（如小型、中型等）
const background = ref(false)//是否使用背景色
const disabled = ref(false)//是否禁用

const sizeChange = (pageSize) => {
  emit('sizeChange', pageSize)
}
const currentChange = (currentPage) => {
  emit('currentChange', currentPage)
}

// 选中行变化事件
const handleSelectionChange = (selectedRows) => {
  emit("selectionChange", selectedRows); // 将选中的行数据传递到父组件
};
</script>

<style scoped>
.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}

.demo-pagination-block {
  margin-bottom: 16px;
}
</style>